<?php

namespace Creattheme\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController {

    public function indexAction() {
        $request = $this->getRequest();
        if ($request->getPost("btncreat") != null) {
            $nameController = ucfirst(strtolower($request->getPost("namecreat"))); //tu dong viet hoa chu cai dau
            $nameTheme = $request->getPost("themeName");


            echo"Running....<br/>";
            echo"----------Coppy Controller-------------<br/>";
            echo "Controller Name :" . $nameController . "<br/>";
            $urltheme = str_replace("\\", "/", APPLICATION_PATH) . "/module/" . $nameTheme . "/src/" . $nameTheme . "/Controller/";
            //Xac dinh duong dan  Controller cua theme muon coppy
            $controllerRoot = $urltheme . "IndexController.php";
            //Xac dinh ten controller theme(Ten controller nay la controller root)
            $controllerNew = $urltheme . $nameController . "Controller.php";

            //Xac dinh controller moi
            $testcoppy = @copy($controllerRoot, $controllerNew); //thuc hien coppy controller
            if ($testcoppy > 0) {
                echo "Da coppy xong.Dang tien hanh xu ly thay doi ten class và thiet lap nhưng thu can thiet...<br/>";
//thuc hien doc tung dung de tim va thay the ten connntroler
                $lines = file($controllerNew);
                $newconten = "";
                foreach ($lines as $line_num => $line) {
                    $newconten .=str_replace("IndexController", $nameController . "Controller", $line);
                }
                $handle = fopen($controllerNew, "r+");
                fwrite($handle, $newconten);
                fclose($handle);
            } else {
                echo "LOi cmnr<br/>";
            }
            echo"----------End coppy controller---------<br/>";

            //coppy view
            $viewThemeName = strtolower($nameTheme);
            $urlView = str_replace("\\", "/", APPLICATION_PATH) . "/module/" . $nameTheme . "/view/" . $viewThemeName . "/index";
            $listFile = $this->listDirs($urlView);
            //khoi tao thu muc controller moi
            $nameControllerView = strtolower($nameController);
            $urlViewNew = str_replace("\\", "/", APPLICATION_PATH) . "/module/" . $nameTheme . "/view/" . $viewThemeName . "/" . $nameControllerView;

            mkdir(APPLICATION_PATH . "/module/" . $nameTheme . "/view/" . $viewThemeName . "/" . $nameControllerView);
            foreach ($listFile["files"] as $keyFile => $dataFile) {
                $urlFile = $listFile["files"][$keyFile];
                $formatfile = str_replace("\\", "/", $urlFile);

                $onlyfile = str_replace($urlView, "", $formatfile);
                //echo $urlViewNew.$onlyfile;die;
                copy($formatfile, $urlViewNew . $onlyfile);
            }
            //end coppy view
            //cau hinh controller file config.
//test config 
            $urlConfig = str_replace("\\", "/", APPLICATION_PATH) . "/module/" . $nameTheme . "/config/module.config.php";


            $lines = file($urlConfig);
            $newconten = "";
          $nameControllerss=  strtolower($nameController);
            $inforController = "'{$nameTheme}\Controller\\$nameController' => '{$nameTheme}\Controller\\{$nameController}Controller',\n\t\t\t\t//flag AddController";
            $inforrouter = "'$nameController' => array(\n" .
                    "'type' => 'segment',\n" .
                    "'options' => array(\n" .
                    "'route' => '/$nameControllerss.fy',\n" .
                    "'defaults' => array(\n" .
                    "'__NAMESPACE__' => '$nameTheme\Controller',\n" .
                    "'controller' => '$nameTheme\Controller\\{$nameControllerss}',\n" .
                    "'action' => 'index',\n" .
                    "),\n" .
                    "),\n" .
                    "),\n//flag addRouter";
            $inforView = "'$nameTheme/$nameControllerss/index' => __DIR__ . '/../view/$nameTheme/$nameControllerss/index.phtml',\n//flag addView";
            foreach ($lines as $line_num => $line) {
                if (strlen(strstr($line, "AddController")) > 0) {
                    $newconten.=str_replace("//flag AddController", $inforController, $line);
                } else {
                    if (strlen(strstr($line, "addRouter")) > 0) {
                        $newconten.=str_replace("//flag addRouter", $inforrouter, $line);
                    } else {
                        if (strlen(strstr($line, "addView")) > 0) {
                            $newconten.=str_replace("//flag addView", $inforView, $line);
                        } else {
                            $newconten.= $line;
                        }
                    }
                }
            }

            $handleabc = fopen($urlConfig, "r+");
            fwrite($handleabc, $newconten);
            fclose($handleabc);
            //end test config 
        }
    }

    private function listDirs($root) {
        $files = array('files' => array(), 'dirs' => array());
        $directories = array();
        $last_letter = $root[strlen($root) - 1];
        $root = ($last_letter == '\\' || $last_letter == '/') ? $root : $root . DIRECTORY_SEPARATOR;

        $directories[] = $root;

        while (sizeof($directories)) {
            $dir = array_pop($directories);
            if ($handle = opendir($dir)) {
                while (false !== ($file = readdir($handle))) {
                    if ($file == '.' || $file == '..') {
                        continue;
                    }
                    $file = $dir . $file;
                    if (is_dir($file)) {
                        $directory_path = $file . DIRECTORY_SEPARATOR;
                        array_push($directories, $directory_path);
                        $files['dirs'][] = $directory_path;
                    } elseif (is_file($file)) {
                        $files['files'][] = $file;
                    }
                }
                closedir($handle);
            }
        }

        return $files;
    }

}
